package com.sumainfor.pc.dev.service.impl;

import com.sumainfor.common.Constants;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.ConvertDateTime;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.dev.dao.CouponMapper;
import com.sumainfor.pc.dev.service.CouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class CouponServiceImpl implements CouponService {
    private ReentrantLock lock = new ReentrantLock(false);
    Logger log = LoggerFactory.getLogger(CouponServiceImpl.class);

    @Autowired
    CouponMapper couponMapper;

    /**
     * <p>
     * 根据优惠券编码获取优惠券信息
     * </p>
     *
     * @author: zhoucong1
     * @since:
     */
    @Override
    public JsonResult getCouponByCouId(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        Integer zc = couponMapper.getCouponByCouIdCount(params);
        System.out.println("在发展部的参数"+zc);
        if (zc>0){
            return result.put(couponMapper.getCouponByCouId(params));
        }else {
            return result.put(new ArrayList<Map<String, Object>>());
        }
    }
    /**
    * <p>
    * 修改优惠券
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult updateCouponStatus(Map<String, Object> params) {
        JsonResult result = new JsonResult();
        if (couponMapper.updateCouponStatus(params) <= 0) {
         return   result.putFailed("修改优惠券状态失败");
        }
         return result.putSuccess("修改优惠券状态成功");
    }

    /**
    * <p>
    * 创建优惠券
    * </p>
    *
    * @author: zhoucong
    * @since:
    */
    @Override
    public JsonResult insertCoupon(Map<String,Object> params){
        JsonResult result = new JsonResult();
        /**
         * 加锁操作
         */
        lock.lock();
        try {
            log.info("增加操作");
            params.put("term", "7");//创建端  1 租客端 2市场部 3 工程部 4运营部 5财务部 6总经办 7发展部
            params.put("fnc",
                    this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
            ParamsCommon.commonInsertParms(params);
            Integer cout = couponMapper.insertCoupon(params);
            if(cout<=0){
                result.putFailed("创建失败!");
            }else{
                result.putSuccess("创建成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
            result.putFailed("服务器繁忙...");
        } finally {
            lock.unlock();
        }
        return result;



    }

}
